package com.clock.service.impl;

import com.clock.common.constant.TagKey;
import com.clock.domain.entity.TagModule;
import com.clock.mapper.TagModuleMapper;
import com.clock.service.TagModuleService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * (TagModule)表服务实现类
 *
 * @author makejava
 * @since 2023-02-28 16:50:48
 */
@Service("tagModuleService")
public class TagModuleServiceImpl implements TagModuleService {

    @Resource
    private TagModuleMapper tagModuleMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<TagModule> getAllTagModule() {
        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(TagKey.TAG_PREFIX + ":" + TagKey.TAG_MODULE_KEY);
        if(!entries.isEmpty()){
            List<TagModule> tagModules = new ArrayList<>();
            Set<Map.Entry<Object, Object>> entrySet = entries.entrySet();
            for (Map.Entry<Object, Object> entry : entrySet) {
                TagModule tagModule = new TagModule(Integer.valueOf((String) entry.getKey()),(String) entry.getValue());
                tagModules.add(tagModule);
            }
            return tagModules;
        }
        List<TagModule> tagModules = tagModuleMapper.queryAll();
        Map map = new HashMap<String,String>();
        for (TagModule tagModule : tagModules) {
            map.put(tagModule.getTagModuleId().toString(),tagModule.getTagModuleName());
        }
        stringRedisTemplate.opsForHash().putAll(TagKey.TAG_PREFIX + ":" + TagKey.TAG_MODULE_KEY,map);
        return tagModules;
    }
}
